package Tests;

import java.util.ArrayList;   
import java.util.List;

import RelCcompiler.*;
import RelUtilityClasses.*;
//import OutputCode.*;

/**
 * 
 * A short testing program for the RelC compiler.
 * 
 * @author Yevgeny Levanzov & Daniel Samuelov
 *
 */
public class TestingProgram {


	/**
	 * The main.
	 * 
	 * @param args - first element - path to relation and function file
	 * 				 second element - path to decomposition graph file.
	 */
	public static void main(String[] args) {

		/* 
		 * the testing is performed in 2 stages:
		 * 
		 * in the first stage, we run the compiler (by making an instance of it) 
		 * and creating the output class, RelImpl, in a new package - OutputCode, 
		 * thus line 8 (import of the package) and
		 * the line of calling test() in main, must be in comments.
		 * 
		 * in the second stage, we un-comment them both, and put create() in a comment.
		 * now we run the tester for the second time, where the test() method contains
		 * some basic tests for the operations. 
		 * the tests are done with the scheduler example (files attached).
		 * 
		 */
		 

		if (args.length != 2) {
			System.err.println("Main must get 2 arguments - paths to relation and decomposition files.");
			System.exit(1);
		}
		
		create(args[0], args[1]);
		
		//test();
		
	}
	
	
	//running the compiler.
	public static void create(String relFilePath, String graphFilePath) {
		
		try {
			
			new CompilerRelC(relFilePath, graphFilePath);   //creating an instance of the compiler.
														    //results in an output java file.

		} catch (SyntaxError e) {
			
			System.out.println(e);
			
		}  catch (SemanticError e) {
		
			System.out.println(e);
		}
		
	} 
		
	//relational operations tests. 
	private static void test() {
		
		try {
			
			RelImpl r = new RelImpl();
			
			//empty relation.
			r.empty();
			
			//insert tests.
			List<String> toInsert = new ArrayList<String>();
			
			toInsert.add("ns:1");
			toInsert.add("pid:1");
			toInsert.add("state:S");
			toInsert.add("cpu:7");
			
			r.insert(toInsert);
		
			toInsert.clear();
			
			toInsert.add("ns:1");
			toInsert.add("pid:2");
			toInsert.add("state:R");
			toInsert.add("cpu:4"); 
			
			r.insert(toInsert); 
			
			System.out.println("after insert:\n" + r);  
			System.out.println();  

			//query tests.
			List<String> q = new ArrayList<String>();
			q.add("ns:1");
			q.add("pid:2");
			
			List<String> cols = new ArrayList<String>();
			cols.add("state");
		
			System.out.println("query:\n");  
			System.out.println(r.query(q, cols));  //should output [state:R]
			System.out.println();  
			
			//update tests.
			cols = new ArrayList<String>();
			cols.add("cpu:22");
			
			r.update(q, cols);
			
			System.out.println("after update:\n" + r);
			System.out.println();  
			
			//remove tests.
			List<String> rem = new ArrayList<String>();
			rem.add("ns:1");
			rem.add("pid:1");
			
			r.remove(rem);
			
			System.out.println("after remove:\n" + r);
			System.out.println();  
	

		} catch (SemanticError e) {
			
			System.out.println(e); 
			
		} 
		
	}
	
	
}
